﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using ePubReader.TestApp.Resources;
using System.Windows.Resources;
using System.Text.RegularExpressions;
using System.Text;

namespace ePubReader.TestApp
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();

            Loaded += OnLoaded;

        }

        void OnLoaded(object sender, RoutedEventArgs e)
        {
            StreamResourceInfo info = Application.GetResourceStream(new Uri("Contents/e.epub", UriKind.Relative));
            using (info.Stream)
            {
                byte[] buffer = new byte[info.Stream.Length];
                info.Stream.Read(buffer, 0, buffer.Length);

                using (Epub epub = new Epub(new System.IO.MemoryStream(buffer)))
                {
                    Content s = epub.GetContent(epub.TOC.First().Source);

                    string temp = @"<html lang=""{3}"">
<head>
    <meta charset=""utf-8"" />
    <meta name=""viewport"" content=""width=320px,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"" />
    <title>{0}</title>
    <style type=""text/css"">
        @media all and (orientation:portrait) {{
            @-ms-viewport {{
                width: 320px;
                user-zoom: fixed;
                max-zoom: 1;
                min-zoom: 1;
            }}
        }}

        @media all and (orientation:landscape) {{
            @-ms-viewport {{
                width: 480px;
                user-zoom: fixed;
                max-zoom: 1;
                min-zoom: 1;
            }}
        }}
        body {{
            margin: 5px;
            font-family: ""Segoe WP"";
            font-size: 11pt;
            padding: 0px;
        }}
    </style>
	{1}
</head>
<body>
    {2}
</body>
</html>";

                    Regex cssbodyRegex = new Regex(@"body\s*\{[^\}]+}", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                    StringBuilder sb = new StringBuilder();
                    foreach (string css in s.Styles)
                    {
                        sb.Append(cssbodyRegex.Replace(css, string.Empty));
                    }

                    string html = string.Format(temp, s.Title, sb, s.Body, epub.Language);
                    browser.NavigateToString(html);


                    // example: Extract
                    try
                    {
                        epub.Extract("epubs/HeftyWater");
                    }
                    catch (System.IO.IOException ex)
                    {
                        System.Diagnostics.Debug.WriteLine(ex.ToString());
                    }
                }
            }
        }


        // Sample code for building a localized ApplicationBar
        //private void BuildLocalizedApplicationBar()
        //{
        //    // Set the page's ApplicationBar to a new instance of ApplicationBar.
        //    ApplicationBar = new ApplicationBar();

        //    // Create a new button and set the text value to the localized string from AppResources.
        //    ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));
        //    appBarButton.Text = AppResources.AppBarButtonText;
        //    ApplicationBar.Buttons.Add(appBarButton);

        //    // Create a new menu item with the localized string from AppResources.
        //    ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
        //    ApplicationBar.MenuItems.Add(appBarMenuItem);
        //}
    }
}